package com.lzx.iteam;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.*;
import com.lzx.iteam.adapter.SalaryTaxAdapter;
import com.lzx.iteam.base.FindViewById;
import com.lzx.iteam.base.MyBaseActivity;
import com.lzx.iteam.bean.SalaryRuleBean;
import com.lzx.iteam.bean.SalaryTaxBean;
import com.lzx.iteam.net.AsynHttpClient;
import com.lzx.iteam.net.GetSalaryRuleMsg;
import com.lzx.iteam.net.LocalLogin;
import com.lzx.iteam.net.WithoutObjMsg;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * **********************************
 * User: zhangshuai
 * Date: 2017年 01月 09日
 * Time: 下午4:53
 *
 * @QQ : 1234567890
 * **********************************
 *
 * 设置个人税务页面
 */
public class SalaryTaxActivity extends MyBaseActivity implements View.OnClickListener,AdapterView.OnItemClickListener{

    @FindViewById(id = R.id.title_ll_back)
    private LinearLayout mLayoutBack;
    @FindViewById(id = R.id.title_tv_center)
    private TextView mTvTitle;
    @FindViewById(id = R.id.title_tv_right)
    private TextView mTvRight;
    @FindViewById(id = R.id.lv_salary_tax)
    private ListView mListView;

    private ArrayList<SalaryTaxBean> mDatas = new ArrayList<>();
    private SalaryTaxAdapter mAdapter;

    private int mTaxState;
    private String mGroupId;

    private final int SET_SALARY_TAX = 30001;
    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            waitDlgDismiss();
            if (msg.arg1 == 0){
                switch (msg.what){
                    case SET_SALARY_TAX:
                        showShortToast("修改成功");
                        finish();
                        break;

                }
            }else{
                Toast.makeText(mActivity, (String) msg.obj,
                      Toast.LENGTH_LONG).show();
            }

        }
    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState,R.layout.salary_tax_layout);
    }

    @Override
    public void initListener() {
        mLayoutBack.setOnClickListener(this);
        mTvRight.setOnClickListener(this);
        mListView.setOnItemClickListener(this);
    }

    @Override
    public void initData() {
        mTvTitle.setText("工资扣税");
        mTvRight.setText("保存");

        Intent intent = getIntent();
        mGroupId = intent.getStringExtra("group_id");
        mTaxState = intent.getIntExtra("salary_tax",1);

        mDatas.add(new SalaryTaxBean(0));
        mDatas.add(new SalaryTaxBean(1));
        mDatas.add(new SalaryTaxBean(2,"3500","3"));
        mDatas.add(new SalaryTaxBean(2,"5000","10"));
        mDatas.add(new SalaryTaxBean(2,"8000","20"));
        mDatas.add(new SalaryTaxBean(2,"12500","25"));
        mDatas.add(new SalaryTaxBean(2,"38500","30"));
        mDatas.add(new SalaryTaxBean(2,"58500","35"));
        mDatas.add(new SalaryTaxBean(2,"83500以上","40"));
        mDatas.add(new SalaryTaxBean(3));
        mDatas.add(new SalaryTaxBean(4));

        mAdapter = new SalaryTaxAdapter(mActivity,mTaxState,mDatas);
        mListView.setAdapter(mAdapter);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.title_ll_back:
                finish();
                break;
            case R.id.title_tv_right:
                setSalaryTax();
                break;
        }
    }

    /**
     * 修改扣税状态
     * @param data
     */
    public void setTaxState(SalaryTaxBean data){
        if (data.getType() == 3){
            mTaxState = 1;
        } else if (data.getType() == 4){
            mTaxState = 0;
        }
        mAdapter.bindData(mTaxState);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        SalaryTaxBean data = mDatas.get(position);
        if (data.getType() == 3){
            mTaxState = 1;
        } else if (data.getType() == 4){
            mTaxState = 0;
        }else{
            return;
        }
        mAdapter.bindData(mTaxState);
    }

    /**
     * 设置工资扣税
     */
    private void setSalaryTax(){
        waitDlgShow();
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair(AsynHttpClient.KEY_SID, LocalLogin
              .getInstance().mSid));
        nvps.add(new BasicNameValuePair(AsynHttpClient.KEY_GROUP_ID, mGroupId));
        nvps.add(new BasicNameValuePair(AsynHttpClient.KEY_SALARY_TAX, mTaxState + ""));
        WithoutObjMsg msg = new WithoutObjMsg(mHandler.obtainMessage(SET_SALARY_TAX));
        msg.mApi = AsynHttpClient.API_SALARY_SET_GROUP_TAX;
        msg.mParams = nvps;
        AsynHttpClient.getInstance(this).execEventHttp(msg);
    }
}
